home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 273_01.zip / GETLINEX.CC < prev    next >
Text File  |  1993-04-04  |  3KB  |  131 lines

  1. #include <tcutil.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. get_linex(int row, int col, char *str, int sleng, int attr, int dleng)
  5. /* This will read a string from the screen starting row,col.
  6.    The String that is read will be put into the string pointed to by *str.
  7.    The length of the string is specified by sleng.
  8.    The length of the display area is specified by dleng.
  9.    Attr specifies the attribute to use when displaying the field.
  10.    RETURNED is the character code that terminated the input string.
  11.    I.E. enter key, esc key, function keys, etc.
  12. */
  13. {
  14.     int ch, ch2, idx=0, tslen=sleng, tdlen=dleng, orow, ocol, ccol;
  15.     int insert=0, lcol, odx=0, lidx, term_ch=0, x, tidx, fidx, flg=0;
  16.     int cur_stat;
  17.  
  18.     locate(row,col);
  19.     cur_stat=get_cur(&orow,&ocol);
  20.     show_cur(1);
  21.     rcolor(orow,ocol,attr,dleng);
  22.     ccol=ocol;
  23.     lcol=ocol + tdlen -1;
  24.     for(x=0; x <= sleng; x++)  {
  25.         if(*(str + x) == 0x00) flg=1;
  26.         if(flg) *(str + x) = ' ';
  27.         else
  28.         if(!isprint(*(str + x))) *(str + x) = ' ';
  29.     }
  30.     *(str+sleng)=0x00;
  31.     writef_n(orow,ocol,attr,str,dleng);
  32.  
  33.     while(1) {
  34.         if(insert) show_cur(9);
  35.         else       show_cur(1);
  36.         ch=get_xa();
  37.         if(isprint(ch)) ch2=0; else ch2=ch;
  38.         switch(ch2) {
  39.             case 0:
  40.                 if(insert) {
  41.                     fidx=sleng - tslen;
  42.                     tidx=sleng - tslen +1;
  43.                     memmove(str+tidx,str+fidx,tslen-1);
  44.                 }
  45.                 *(str + idx) = ch;
  46.                 idx++; ccol++; tslen--;
  47.                 break;
  48.             case K_ENTER:
  49.             case K_ESC:
  50.                 term_ch=ch;
  51.                 goto end_it;
  52.             case K_RIGHT:
  53.                 idx++; ccol++; tslen--;
  54.                 break;
  55.             case K_HOME:
  56.                 idx=odx=0;
  57.                 tslen=sleng;
  58.                 ccol=ocol;
  59.                 break;
  60.             case K_END:
  61.                 for(idx=sleng - 1; idx >=0 ;idx--) {
  62.                     if(*(str + idx) != ' ') {
  63.                         idx++;
  64.                         break;
  65.                     }
  66.                 }
  67.                 tslen = sleng - idx;
  68.                 if(idx + 1 <= dleng) {
  69.                     odx = 0;
  70.                     ccol = ocol + idx;
  71.                 }
  72.                 else {
  73.                     odx = idx - dleng + 1;
  74.                     ccol = lcol;
  75.                 }
  76.                 break;
  77.             case K_BACKSPACE:
  78.                 if(idx) idx--;
  79.                 tslen++;
  80.                 ccol--;
  81.                 *(str + idx) = ' ';
  82.                 break;
  83.             case K_LEFT:
  84.                 idx--; tslen++;
  85.                 ccol--;
  86.                 break;
  87.             case K_CEND:
  88.                 for(x=idx; x < sleng; x++)
  89.                     *(str + x) = ' ';
  90.                 break;
  91.             case K_DEL:
  92.                 if(!tslen)
  93.                     *(str + idx) = ' ';
  94.                 else {
  95.                     tidx=sleng-tslen;
  96.                     fidx=sleng-tslen + 1;
  97.                     memmove(str+tidx,str+fidx,tslen);
  98.                     *(str+sleng -1)=' ';
  99.                 }
  100.                 break;
  101.             case K_INS:
  102.                 insert = insert ^ 0x01;
  103.                 break;
  104.             default:
  105.                 term_ch=ch;
  106.                 goto end_it;
  107.         }
  108.         if(tslen > sleng) tslen=sleng;
  109.         if(tslen < 0) tslen=0;
  110.         if(idx < 0) idx=0;
  111.         if(idx > sleng - 1) idx=sleng-1;
  112.         if(ccol > lcol) {
  113.             ccol=lcol;
  114.             odx++;
  115.         }
  116.         if(ccol < ocol) {
  117.             ccol=ocol;
  118.             odx--;
  119.         }
  120.         if(odx < 0) odx=0;
  121.         if(odx > (sleng-dleng)) odx=sleng-dleng;
  122.         writef_n(orow,ocol,attr,str+odx,dleng);
  123.         locate(orow,ccol);
  124.     }
  125. end_it:
  126.     trim_r(str);
  127.     if(!term_ch) term_ch=0x0d;
  128.     if(cur_stat) hide_cur();
  129.     return(term_ch);
  130. }
  131.